import 'package:flutter/widgets.dart';

class LYGradientBorderContainer extends StatelessWidget {
  final double borderWidth;
  final Gradient gradient;
  final Widget child;
  final Radius radius;

  const LYGradientBorderContainer(
      {super.key,
      required this.gradient,
      required this.child,
      this.borderWidth = 0.5,
      this.radius = const Radius.circular(8.0)});

  @override
  Widget build(BuildContext context) {
    return CustomPaint(
      painter: GradientBorderPainter(
        borderWidth: borderWidth,
        gradient: gradient,
        radius: radius,
      ),
      child: child,
    );
  }
}

class GradientBorderPainter extends CustomPainter {
  final double borderWidth;
  final Gradient gradient;
  final Radius radius;

  GradientBorderPainter({
    required this.borderWidth,
    required this.gradient,
    required this.radius,
  });

  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()
      ..shader =
          gradient.createShader(Rect.fromLTWH(0, 0, size.width, size.height))
      ..style = PaintingStyle.stroke
      ..strokeWidth = borderWidth;

    final rect = Rect.fromLTWH(0, 0, size.width, size.height);
    final rRect = RRect.fromRectAndRadius(rect, radius);
    canvas.drawRRect(rRect, paint);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return false;
  }
}
